package com.digiproto.app_store;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digiproto.app_store.R;
import com.digiproto.app_store.model.Game;
import com.digiproto.app_store.utils.ImageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import org.w3c.dom.Text;

import java.io.Console;
import java.util.List;

/**
 * Created by TreeNode on 2017/11/8.
 * 数据跟视图传输组件
 */

public class GameListAdapter extends BaseAdapter {

    private List<Game> mData;
    private Context mContext;

    public GameListAdapter(List<Game> mGame, Context mContext){
        this.mData = mGame;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false);
            // 排列序号
            TextView listDesc = (TextView) convertView.findViewById(R.id.item_text);
            // 头部head
            ImageView gameIcon = (ImageView) convertView.findViewById(R.id.item_img);
            // 游戏名称
            TextView gameName = (TextView) convertView.findViewById(R.id.game_head_name);
            // 游戏大小类型
            TextView gameTypeSize = (TextView)convertView.findViewById(R.id.game_type_size);
            // 游戏热度值
            TextView gameHot = (TextView) convertView.findViewById(R.id.game_hot);
            // 设置游戏序号
            listDesc.setText(mData.get(position).getGameId());
            // 设置游戏头像
            ImageSize mImageSize = new ImageSize(64, 64);

            ImageLoader.getInstance().displayImage(mData.get(position).getGameIconUrl(),gameIcon);

            // gameIcon.setImageBitmap(ImageUtils.getHttpBitMap(mData.get(position).getGameIconUrl()));
            /**
            ImageLoader.getInstance().loadImage(mData.get(position).getGameIconUrl(), mImageSize, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    gameIcon.setImageBitmap(loadedImage);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });
             */
            // 设置游戏名称
            gameName.setText(mData.get(position).getGameName());
            // 设置类型和大小
            gameTypeSize.setText(mData.get(position).getGameType()+" | "+mData.get(position).getGameSize());
            // 设置hot值
            gameHot.setText("热度值： "+mData.get(position).getGameHot());
        }
        return convertView;
    }
}
